package com.tanhua.sso.controller;

import com.tanhua.sso.service.SmsService;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("user")
@Slf4j
public class SmsController {

    @Autowired
    private SmsService smsService;

    /**
     *  发送短信验证码
     * @param param
     * @return
     */
    @RequestMapping("login")
    public ResponseEntity<ErrorResult> sendCheckCode(@RequestBody Map<String,String> param){

        //获取手机号
        String phone = param.get("phone");
        ErrorResult result=null;

        try {

            result= this.smsService.sendCheckCode(phone);

            //结果返回空表示发送成功 向前端反馈200
            if(result==null){
                //如果只是返回一个状态码为200的内容，可以简写ResponseEntity.ok(T)
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {

            log.error("短信验证码发送失败..."+phone,e);

            result = ErrorResult.builder().errCode("000002").errMessage("短信验证发送失败").build();

        }

        //发送失败
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
    }


}
